﻿using System;
using System.Collections.Generic;
using System.Text;
using QidorSpider.Components.Util;
using System.Data;

namespace QidorSpider.Components.Data
{
    public class SpiderDAL
    {
        #region DatabaseProperty
        private static DatabaseProperty accessProperty
        {
            get
            {
                DatabaseProperty defaultProperty = new DatabaseProperty();
                defaultProperty.ConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}\\ET.mdb;",
                                                                 AppDomain.CurrentDomain.BaseDirectory.ToString());
                defaultProperty.DatabaseType = DatabaseType.OleDBSupported;
                return defaultProperty;
            }
        }

        private static DatabaseProperty mssqlProperty
        {
            get
            {
                DatabaseProperty defaultProperty = new DatabaseProperty();
                defaultProperty.ConnectionString = string.Format("server=foolnet-3fa9be8;database=WebsharpTest;uid=sa;pwd=",
                                                                 AppDomain.CurrentDomain.BaseDirectory.ToString());
                defaultProperty.DatabaseType = DatabaseType.OleDBSupported;
                return defaultProperty;
            }
        }

        private static DatabaseProperty oracleProperty
        {
            get
            {
                DatabaseProperty defaultProperty = new DatabaseProperty();
                defaultProperty.ConnectionString = string.Format("Data Source=websharp;user id=foolnet;pwd=foolnet",
                                                                 AppDomain.CurrentDomain.BaseDirectory.ToString());
                defaultProperty.DatabaseType = DatabaseType.OleDBSupported;
                return defaultProperty;
            }
        }
        #endregion

        private static string sql = "";
        private static DataAccess dataAccess = DataAccessFactory.CreateDataAccess(accessProperty);

        #region GetClassDataList
        /// <summary>
        /// get class list
        /// </summary>
        /// <param name="classType"></param>
        /// <returns></returns>
        public static List<ClassData> GetClassDataList(Common.ClassType classType)
        {
            try
            {
                List<ClassData> classList = new List<ClassData>();

                sql = string.Format("select * from glist where g_type='{0}' order by g_sn desc", classType.ToString());
                dataAccess.Open();

                DataTable classTable = dataAccess.ExecuteDataset(sql).Tables[0];
                if(classTable != null && classTable.Rows.Count > 0)
                {
                    foreach(DataRow row in classTable.Rows)
                    {
                        ClassData classData = new ClassData();
                        classData.g_id = int.Parse(row["g_id"].ToString());
                        classData.g_type = row["g_type"].ToString();
                        classData.g_name = row["g_name"].ToString();
                        classData.g_sn = int.Parse(row["g_sn"].ToString());

                        classList.Add(classData);
                    }
                }

                return classList;
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
                if(!dataAccess.IsClosed)
                    dataAccess.Close();
            }
        }
        #endregion

        #region InsClassData
        /// <summary>
        /// insert class data
        /// </summary>
        /// <param name="classData"></param>
        public static void InsClassData(ClassData classData)
        {
            try
            {
                sql = string.Format("insert into glist(g_type, g_name, g_sn) values({0},{1},{2}", classData.g_type, classData.g_name, classData.g_sn);
                dataAccess.Open();

                dataAccess.ExecuteNonQuery(sql);
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
                if(!dataAccess.IsClosed)
                    dataAccess.Close();
            }
        }
        #endregion

        #region UpdClassData
        /// <summary>
        /// update class data
        /// </summary>
        /// <param name="classData"></param>
        public static void UpdClassData(ClassData classData)
        {
            try
            {
                sql = string.Format("update glist set g_type={0}, g_name={1}, g_sn={2} where g_id={3} ", classData.g_type, classData.g_name, classData.g_sn, classData.g_id);
                dataAccess.Open();

                dataAccess.ExecuteNonQuery(sql);
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
                if(!dataAccess.IsClosed)
                    dataAccess.Close();
            }
        }
        #endregion

        #region DelClassData
        /// <summary>
        /// delete class data by classid
        /// </summary>
        /// <param name="classID"></param>
        public static void DelClassData(int classID)
        {
            try
            {
                sql = string.Format("delete glist where g_id={0}", classID);
                dataAccess.Open();

                dataAccess.ExecuteNonQuery(sql);
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
                if(!dataAccess.IsClosed)
                    dataAccess.Close();
            }
        }
        #endregion

        #region GetPlanList
        /// <summary>
        /// get plan list
        /// </summary>
        /// <param name="classID"></param>
        /// <returns></returns>
        public static List<PlanData> GetPlanList(int classID, int planID)
        {
            try
            {
                List<PlanData> planDataList = new List<PlanData>();

                sql = "select * from plan where 1=1";

                if(classID != 0)
                {
                    sql += " and g_id =" + classID;
                }

                if(planID != 0)
                {
                    sql += " and id=" + planID;
                }

                sql += " order by id desc";

                DataTable planTable = dataAccess.ExecuteDataset(sql).Tables[0];
                if(planTable != null && planTable.Rows.Count > 0)
                {
                    foreach(DataRow row in planTable.Rows)
                    {
                        PlanData planData = new PlanData();

                        planData.id = int.Parse(row["id"].ToString());
                        planData.name = row["name"].ToString();
                        planData.rulecj = int.Parse(row["rulecj"].ToString());
                        planData.rulemg = int.Parse(row["rulemg"].ToString());
                        planData.rulepost = int.Parse(row["rulepost"].ToString());
                        planData.ymdir = (bool)row["ymdir"];
                        planData.deldownfile = (bool)row["deldownfile"];
                        planData.forcepost = (bool)row["forcepost"];
                        planData.selfchk = (bool)row["selfchk"];
                        planData.chkmode = int.Parse(row["chkmode"].ToString());
                        planData.prename = row["prename"].ToString();
                        planData.filepath = row["filepath"].ToString();
                        planData.postsort = (bool)row["postsort"];
                        planData.plandelay = int.Parse(row["plandelay"].ToString());
                        planData.istime = (bool)row["istime"];
                        planData.autotime = row["autotime"].ToString();
                        planData.sn = int.Parse(row["sn"].ToString());
                        planData.g_id = int.Parse(row["g_id"].ToString());

                        planDataList.Add(planData);
                    }
                }

                return planDataList;
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
                if(!dataAccess.IsClosed)
                    dataAccess.Close();
            }
        }
        #endregion

        #region GetKeyDataList
        /// <summary>
        /// get plan list
        /// </summary>
        /// <param name="classID"></param>
        /// <returns></returns>
        public static List<KeyData> GetKeyDataList(int classID, int keyID)
        {
            try
            {
                List<KeyData> keyDataList = new List<KeyData>();

                sql = "select * from rule_key where 1=1";

                if (classID != 0)
                {
                    sql += " and g_id =" + classID;
                }

                if (keyID != 0)
                {
                    sql += " and id=" + keyID;
                }

                sql += " order by sn desc";

                DataTable keyTable = dataAccess.ExecuteDataset(sql).Tables[0];
                if (keyTable != null && keyTable.Rows.Count > 0)
                {
                    foreach (DataRow row in keyTable.Rows)
                    {
                        KeyData keyData = new KeyData();

                        keyData.id = int.Parse(row["id"].ToString());
                        keyData.name = row["name"].ToString();
                        keyData.mg_title = row["mg_title"].ToString();
                        keyData.key_title = row["key_title"].ToString();
                        keyData.mg_content = row["mg_content"].ToString();
                        keyData.imgmark = (bool)row["imgmark"];
                        keyData.imgmarkfilename = row["imgmarkfilename"].ToString();
                        keyData.imgwz = int.Parse(row["imgwz"].ToString());
                        keyData.imgalpha = int.Parse(row["imgalpha"].ToString());
                        keyData.logow = double.Parse(row["logow"].ToString());
                        keyData.logoh = double.Parse(row["logoh"].ToString());
                        keyData.sn = int.Parse(row["sn"].ToString());

                        keyDataList.Add(keyData);
                    }
                }

                return keyDataList;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (!dataAccess.IsClosed)
                    dataAccess.Close();
            }
        }
        #endregion

        #region InsKeyData
        /// <summary>
        /// insert Key data
        /// </summary>
        /// <param name="classData"></param>
        public static void InsKeyData(KeyData keyData)
        {
            try
            {
                sql = string.Format("insert into rule_key(name,mg_title,key_title,mg_content,imgmark,imgmarkfilename,imgwz,imgalpha,logow,logoh,sn) values({0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11}",
                    keyData.mg_title,
                    keyData.key_title,
                    keyData.mg_content,
                    keyData.imgmark,
                    keyData.imgmarkfilename,
                    keyData.imgwz,
                    keyData.imgalpha,
                    keyData.logow,
                    keyData.logoh,
                    keyData.sn);

                dataAccess.Open();

                dataAccess.ExecuteNonQuery(sql);
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
                if(!dataAccess.IsClosed)
                    dataAccess.Close();
            }
        }
        #endregion

        #region UpdKeyData
        /// <summary>
        /// update key data
        /// </summary>
        /// <param name="classData"></param>
        public static void UpdKeyData(KeyData keyData)
        {
            try
            {
                sql = string.Format("update rule_key set name={0},mg_title={1},key_title={2},mg_content={3},imgmark={4},imgmarkfilename={5},imgwz={6},imgalpha={7},logow={8},logoh={9},sn={10} where id={11} ",
                    keyData.mg_title,
                    keyData.key_title,
                    keyData.mg_content,
                    keyData.imgmark,
                    keyData.imgmarkfilename,
                    keyData.imgwz,
                    keyData.imgalpha,
                    keyData.logow,
                    keyData.logoh,
                    keyData.sn,
                    keyData.id);

                dataAccess.Open();

                dataAccess.ExecuteNonQuery(sql);
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
                if(!dataAccess.IsClosed)
                    dataAccess.Close();
            }
        }
        #endregion

        #region DelKeyData
        /// <summary>
        /// delete key data by id
        /// </summary>
        /// <param name="classID"></param>
        public static void DelKeyData(int keyID)
        {
            try
            {
                sql = string.Format("delete rule_key where id={0}", keyID);
                dataAccess.Open();

                dataAccess.ExecuteNonQuery(sql);
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
                if(!dataAccess.IsClosed)
                    dataAccess.Close();
            }
        }
        #endregion
    }
}
